package com.ti.winona.ui.fragment;

import org.json.JSONObject;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.taplinker.client.App;
import com.taplinker.client.AppConfigFactory;
import com.taplinker.client.R;
import com.ti.winona.ConstantValue;
import com.ti.winona.net.API;
import com.ti.winona.net.RequestClient;
import com.ti.winona.ui.LoginActivity;
import com.ti.winona.util.FragmentOperateUtil;
import com.ti.winona.util.SharedPreferencesUtil;
import com.ti.winona.widget.LoadingDialog;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

public class SystemSetFragment extends Fragment {

	public static final String TAG = "SystemSetFragment";
	
	private View mSystemSetView;
	
	private LoadingDialog mLoadingDialog;
	
	private ImageView mImgSystemSetBack;
	
	private ImageView mImgSystemsetSwitchPatientWarn;
	private ImageView mTxtSystemsetSwitchDoctorWarn;
	private ImageView mTxtSystemsetSwitchAcademicNews;
	
	private Button mBtnSystemSetLoginout;
	
	private long mSettingId;
	private boolean mSwitchBtValue[] = {false, false, false};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return mSystemSetView = inflater.inflate(R.layout.fragment_systemset, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		
		findView();
		initDialog();
		initSetSwitth();
		initListener();
	}
	
	private void findView() {
		mImgSystemSetBack = (ImageView) mSystemSetView.findViewById(R.id.img_systemset_back);
		
		mImgSystemsetSwitchPatientWarn = (ImageView) mSystemSetView.findViewById(R.id.img_systemset_switch_patient_warn);
		mTxtSystemsetSwitchDoctorWarn = (ImageView) mSystemSetView.findViewById(R.id.txt_systemset_switch_doctor_warn);
		mTxtSystemsetSwitchAcademicNews = (ImageView) mSystemSetView.findViewById(R.id.txt_systemset_switch_academic_news);
		
		mBtnSystemSetLoginout = (Button) mSystemSetView.findViewById(R.id.btn_systemset_loginout);
	}
	
	private void initDialog() {
		mLoadingDialog = new LoadingDialog(getActivity());
		mLoadingDialog.setCancelable(true);
		mLoadingDialog.setCanceledOnTouchOutside(false);
	}
	
	private void initSetSwitth() {
		RequestParams params = new RequestParams();
		params.put("userid", AppConfigFactory.getInstance().getUserInfo().getUserid()+"");
		
		RequestClient.get(getActivity(), API.SYSTEM_SET, params, new AsyncHttpResponseHandler(){

			@Override
			public void onStart() {
				super.onStart();
				mLoadingDialog.setText("正在初始化...");
				mLoadingDialog.show();
			}

			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				App.showLog("log_ktian", "获取到的设置信息："+content);
				try {
					if("OK".equalsIgnoreCase( new JSONObject(content).getString("code") )) {
						JSONObject jsonObject = new JSONObject(content).getJSONObject("body");
						mSettingId = jsonObject.getLong("id");
						
						mSwitchBtValue[0] = (1 == jsonObject.getInt("patientsRemind"));
						mSwitchBtValue[1] = (1 == jsonObject.getInt("doctorRemind"));
						mSwitchBtValue[2] = (1 == jsonObject.getInt("orderactivityRemind"));
						setSwitchBtn();
					}else{
						App.showToast("获取数据失败, 请重试!");
					}
				} catch (Exception e) {
					e.printStackTrace();
					App.showToast("获取数据失败, 请重试!");
				}
			}
			
			@Override
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				App.showLog("log_ktian", "获取设置信息失败："+content);
			}

			@Override
			public void onFinish() {
				super.onFinish();
				mLoadingDialog.dismiss();
			}
			
		});
	}
	
	private void initListener() {
		// 返回
		mImgSystemSetBack.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				FragmentOperateUtil.popBackAllStack(getActivity().getSupportFragmentManager());
			}
		});
		// 退出登录
		mBtnSystemSetLoginout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 开始退出登陆
				getActivity().startActivity(new Intent(getActivity(), LoginActivity.class));
				getActivity().finish();
				
				// 清除密码
				clearPwd();
			}
		});
		// 更改患者咨询提醒
		mImgSystemsetSwitchPatientWarn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				doUpdate(!mSwitchBtValue[0], mSwitchBtValue[1], mSwitchBtValue[2]);
			}
		});
		// 更改医生问询提醒
		mTxtSystemsetSwitchDoctorWarn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				doUpdate(mSwitchBtValue[0], !mSwitchBtValue[1], mSwitchBtValue[2]);
			}
		});
		// 更改学术动态提醒
		mTxtSystemsetSwitchAcademicNews.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				doUpdate(mSwitchBtValue[0], mSwitchBtValue[1], !mSwitchBtValue[2]);
			}
		});
	}
	
	private void setSwitchBtn() {
		mImgSystemsetSwitchPatientWarn.setSelected(mSwitchBtValue[0]);
		mTxtSystemsetSwitchDoctorWarn.setSelected(mSwitchBtValue[1]);
		mTxtSystemsetSwitchAcademicNews.setSelected(mSwitchBtValue[2]);
	}
	
	private void clearPwd() {
		SharedPreferencesUtil.putString(getActivity(), ConstantValue.sharedpreferences.SHARED_NAME, ConstantValue.sharedpreferences.USER_PASSWORD, "");
	}
	
	private void doUpdate(final boolean bool1, final boolean bool2, final boolean bool3) {
		RequestParams params = new RequestParams();
		params.put("userid", AppConfigFactory.getInstance().getUserInfo().getUserid()+"");
		params.put("id", mSettingId+"");
		params.put("patientsRemind", bool1 ? "1" : "0");
		params.put("doctorRemind", bool2 ? "1" : "0");
		params.put("orderactivityRemind", bool3 ? "1" : "0");
		
		RequestClient.get(getActivity(), API.SYSTEM_SET_UPDATE, params, new AsyncHttpResponseHandler(){

			@Override
			public void onStart() {
				super.onStart();
				mLoadingDialog.setText("提交数据...");
				mLoadingDialog.show();
			}

			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				
				mSwitchBtValue[0] = bool1;
				mSwitchBtValue[1] = bool2;
				mSwitchBtValue[2] = bool3;
				setSwitchBtn();
			}
			
			@Override
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				App.showLog("log_ktian", "更新设置信息失败："+content);
			}

			@Override
			public void onFinish() {
				super.onFinish();
				mLoadingDialog.dismiss();
			}
			
		});
		
	}
	
}
